from tkinter import *

root = Tk()
root.title('计算器')
root.geometry('330x330+100+100')
root.config(bg='#dcdcdc')

result_num = StringVar()
result_num.set('0')

# 界面布局
show_result = Label(root,textvariable=result_num,bg='#dcdcdc',font=('宋体',20),height=3,width=22,anchor=SE)
show_result.grid(row=0,column=0,columnspan=4)

# 按钮
# clear
button_clear = Button(root,text='C',bg='#b2b2b2',font=('宋体',18),relief=FLAT,width=5)
# 返回
button_back = Button(root,text='←',bg='#b2b2b2',font=('宋体',18),relief=FLAT,width=5)
# 除号
button_div = Button(root,text='/',bg='#b2b2b2',font=('宋体',18),relief=FLAT,width=5)
# 乘号
button_mult = Button(root,text='*',bg='#b2b2b2',font=('宋体',18),relief=FLAT,width=5)

button_clear.grid(row=1,column=0,padx=4,pady=2)
button_back.grid(row=1,column=1,padx=4,pady=2)
button_div.grid(row=1,column=2,padx=4,pady=2)
button_mult.grid(row=1,column=3,padx=4,pady=2)

# 7
button_7 = Button(root,text='7',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# 8
button_8 = Button(root,text='8',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# 9
button_9 = Button(root,text='9',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# -
button_sub = Button(root,text='-',bg='#b2b2b2',font=('宋体',18),relief=FLAT,width=5)

button_7.grid(row=2,column=0,padx=4,pady=2)
button_8.grid(row=2,column=1,padx=4,pady=2)
button_9.grid(row=2,column=2,padx=4,pady=2)
button_sub.grid(row=2,column=3,padx=4,pady=2)

# 4
button_4 = Button(root,text='4',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# 5
button_5 = Button(root,text='5',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# 6
button_6 = Button(root,text='6',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# +
button_add = Button(root,text='+',bg='#b2b2b2',font=('宋体',18),relief=FLAT,width=5)

button_4.grid(row=3,column=0,padx=4,pady=2)
button_5.grid(row=3,column=1,padx=4,pady=2)
button_6.grid(row=3,column=2,padx=4,pady=2)
button_add.grid(row=3,column=3,padx=4,pady=2)

# 1
button_1 = Button(root,text='1',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# 2
button_2 = Button(root,text='2',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# 3
button_3 = Button(root,text='3',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# =
button_equl = Button(root,text='=',bg='#b2b2b2',font=('宋体',18),relief=FLAT,width=5,height=3)

button_1.grid(row=4,column=0,padx=4,pady=2)
button_2.grid(row=4,column=1,padx=4,pady=2)
button_3.grid(row=4,column=2,padx=4,pady=2)
button_equl.grid(row=4,column=3,padx=4,pady=2,rowspan=2)

# 0
button_0 = Button(root,text='0',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=12)
# 点号
button_point = Button(root,text='.',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# button_00 = Button(root,text='0',bg='#edcd9e',font=('宋体',18),relief=FLAT,width=5)
# button_equl2 = Button(root,text='=',bg='#b2b2b2',font=('宋体',18),relief=FLAT,width=5)

button_0.grid(row=5,column=0,padx=4,pady=2,columnspan=2)
button_point.grid(row=5,column=2,padx=4,pady=2)
# button_00.grid(row=5,column=1,padx=4,pady=2)
# button_equl2.grid(row=5,column=3,padx=4,pady=2)


# 逻辑运算
def click(value):
    # print(value)
    cur_text = result_num.get()
    if cur_text == '0':
        result_num.set(value)
    else:
        # 取出计算器中的最后一个字符
        sign = cur_text[-1]
        if '+-*/.'.find(sign) >= 0 and '+-*/.'.find(value) >= 0:
            # 不能同时有两个运算符在一起使用
            pass
        else:
            result_num.set(cur_text + value)

button_0.config(command=lambda :click('0'))
button_1.config(command=lambda :click('1'))
button_2.config(command=lambda :click('2'))
button_3.config(command=lambda :click('3'))
button_4.config(command=lambda :click('4'))
button_5.config(command=lambda :click('5'))
button_6.config(command=lambda :click('6'))
button_7.config(command=lambda :click('7'))
button_8.config(command=lambda :click('8'))
button_9.config(command=lambda :click('9'))
button_add.config(command=lambda :click('+'))
button_sub.config(command=lambda :click('-'))
button_mult.config(command=lambda :click('*'))
button_div.config(command=lambda :click('/'))
button_point.config(command=lambda :click('.'))


# 计算函数
def calculation():
    opt_str = result_num.get()
    try:
        find_div = opt_str.find('/')
        if find_div != -1:
            result = opt_str.split('/')
            if result[1] == 0:
                raise ZeroDivisionError()
        result = eval(opt_str)
        result_num.set(result)
    except ZeroDivisionError as z:
        result_num.set('除数为零，运算失败')
    except Exception as e:
        result_num.set('Error,内容有误')



button_equl.config(command=calculation)

# 回退函数
def back():
    cur_text = result_num.get()
    if len(cur_text) != 1:
        result_num.set(cur_text[0:len(cur_text)-1])
    else:
        result_num.set('0')

button_back.config(command=back)

# 清空函数
def clear():
    result_num.set('0')
button_clear.config(command=clear)


root.mainloop()